﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;

public partial class main_uc_contact_main : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (!Page.IsPostBack)
        {
            LoadPage();
        }
        SetProperties();
        if (Session["message"] != null && Session["message"] != "")
        {
            ContentPlaceHolder1.Controls.Add(new LiteralControl("<span class='" + Session["message_type_css"].ToString() + "'>"
                    + Session["message"].ToString() + "</span>"));
            Session["message"] = null;
            Session["message_type_css"] = null;
        }
    }

    private void SetProperties()
    {
        tb_comp_name.Attributes.Add("validate", "true");
        tb_contact_email.Attributes.Add("validate", "true");
        tb_contact_person.Attributes.Add("validate", "true");
        tb_contact_subject.Attributes.Add("validate", "true");
        tb_country_region.Attributes.Add("validate", "true");
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    private void LoadPage()
    {
        string text;
        string keywords;
        Utilities.LoadPage(this.LangId, "contact", out text, out keywords);
        lblText.Text = text;
    }
    protected void ESi18nButton1_Click(object sender, EventArgs e)
    {
        //get mail settings
        string senderMailAdress = ConfigurationManager.AppSettings["senderMailAdress"].ToString();
        string adminMail = ConfigurationManager.AppSettings["adminMail"].ToString();
        string host = ConfigurationManager.AppSettings["host"].ToString();
        //string username = ConfigurationManager.AppSettings["username"].ToString();
        //string password = ConfigurationManager.AppSettings["password"].ToString();

        //create new mail message
        MailMessage mail = new MailMessage();

        //create mail message body
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.UTF8;

        sb.Append("<html><head>");
        sb.Append("<style> body{font-family: \"Calibri\",\"sans-serif\";font-size: 11pt;margin: 0 0 0.0001pt;}</style>");
        sb.Append("</head><body>");

        sb.Append("Contact Administrator form,");
        sb.Append("<br><br>");
        sb.Append("<p>Dear administrator, a customer from the transportexchange.org has submitted the following information from the contact administrator form</p>");
        sb.Append("<strong>Company Name:</strong> " + tb_comp_name.Text + "<br>");
        sb.Append("<strong>Country of Origin:</strong> " + tb_country_region.Text + "<br>");
        sb.Append("<strong>Contact Person:</strong> " + tb_contact_person.Text + "<br>");
        sb.Append("<strong>Email:</strong> " + tb_contact_email.Text + "<br>");
        sb.Append("<strong>Subject:</strong> " + tb_contact_subject.Text + "<br>");
        sb.Append("<strong>Message:</strong> " + tb_contact_message.Text + "<br>");
        sb.Append("<br>");
        sb.Append("<p>Sincerely,</p><br />");
        sb.Append("<p><font style=\"font-size: 14px; color:#081f59;\"></font><strong>Transportexchange.org</strong><br />");
        sb.Append("</body></html>");
        mail.Body = sb.ToString();

        //set mail message properties
        mail.To.Add(new MailAddress(senderMailAdress));
        //mail.Bcc.Add("ratkop@gmail.com");
        mail.From = new MailAddress(adminMail);
        mail.Subject = "TransportExchange.org Registration";

        //create new SmtpClient
        SmtpClient smtpClient = new SmtpClient();

        //set SmtpClient properties
        smtpClient.Port = 25;
        smtpClient.Host = host;
        string username = ConfigurationManager.AppSettings["username"].ToString();
        string password = ConfigurationManager.AppSettings["password"].ToString();
        smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
        try
        {
            smtpClient.Send(mail);
            Session["message"] = "Your request has been submitted to our administrators, We will contact you very soon. Thank You.";
            Session["message_type_css"] = "par_message_ok";
        }
        catch (Exception ex)
        {
            Session["message"] = "There was an error submiting your request "+ ex.ToString();
            Session["message_type_css"] = "par_message_error";
        }
        Response.Redirect(Request.Url.ToString());
    }
}
